//
//  YDDoublePickerAlertView.m
//  YunDongGym
//
//  Created by lqq on 2018/7/20.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDDoublePickerAlertView.h"

@interface YDDoublePickerAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *leftTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightTitleLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *leftPickerView;
@property (weak, nonatomic) IBOutlet UIPickerView *rightPickerView;
@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;


@property (nonatomic, strong) NSArray *leftDataSource;
@property (nonatomic, assign) NSInteger leftDefaultIndex;
@property (nonatomic, copy) NSString *leftTitle;//
@property (nonatomic, strong) NSArray *rightDataSource;
@property (nonatomic, assign) NSInteger rightDefaultIndex;
@property (nonatomic, copy) NSString *rightTitle;//
@property(nonatomic ,copy) void(^selectBlock)(NSInteger leftSelectIndex,NSInteger rightSelectIndex);
@end

@implementation YDDoublePickerAlertView
+ (void)showDoublePickerWithLeftDataSource:(NSArray *)leftDataSource
                          leftDefaultIndex:(NSInteger)leftDefaultIndex
                                 leftTitle:(NSString *)leftTitle
                           rightDataSource:(NSArray *)rightDataSource rightDefaultIndex:(NSInteger)rightDefaultIndex rightTitle:(NSString *)rightTitle
                               selectBlock:(void(^)(NSInteger leftSelectIndex,NSInteger rightSelectIndex))selectBlock
{
    YDDoublePickerAlertView *alertView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YDDoublePickerAlertView class]) owner:nil options:nil] lastObject];
    alertView.leftDataSource = leftDataSource;
    alertView.leftDefaultIndex = leftDefaultIndex;
    alertView.leftTitle = leftTitle;
    alertView.rightDataSource = rightDataSource;
    alertView.rightDefaultIndex = rightDefaultIndex;
    alertView.rightTitle = rightTitle;
    alertView.selectBlock = selectBlock;
    [alertView show];
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    _leftPickerView.delegate = self;
    _leftPickerView.dataSource = self;
    _rightPickerView.delegate = self;
    _rightPickerView.dataSource = self;
    _cancleBtn.layer.cornerRadius = 2;
    _cancleBtn.layer.masksToBounds = YES;
    [_cancleBtn addBorderWithBorderWidth:kOnePX borderColor:HEXColor(@"#9B9B9B")];
    _confirmBtn.layer.cornerRadius = 2;
    _confirmBtn.layer.masksToBounds = YES;
}

- (void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    
    _leftTitleLabel.text = _leftTitle;
    _rightTitleLabel.text = _rightTitle;
    
    [_leftPickerView selectRow:_leftDefaultIndex inComponent:0 animated:NO];
    [_rightPickerView selectRow:_rightDefaultIndex inComponent:0 animated:NO];
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }];
}
- (IBAction)cancleBtnClicked:(id)sender {
    [self removeFromSuperview];
}
- (IBAction)confirmBtnClicked:(id)sender {
    NSInteger leftIndex = [self.leftPickerView selectedRowInComponent:0];
    NSInteger rightIndex = [self.rightPickerView selectedRowInComponent:0];
    if (_selectBlock) {
        _selectBlock(leftIndex,rightIndex);
    }
    [self removeFromSuperview];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == _leftPickerView) {
        return _leftDataSource.count;
    } else if (pickerView == _rightPickerView) {
        return _rightDataSource.count;
    }
    return 0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = [UIColor colorWithHexString:@"DFDFDF"];
        }
    }
    
    //设置文字的属性
    UILabel *label = [UILabel new];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    
    if (pickerView == _leftPickerView) {
        label.text = _leftDataSource[row];
    } else if (pickerView == _rightPickerView) {
        label.text = _rightDataSource[row];
    }
    
    
    
    return label;
}
@end
